import MD5 from '../node_modules/md5-js/md5.js';

// 加入书架
const addshujia = (data) => {
	var info = {
		"book_id": data.book_id,
		"img": data.img,
		"book_name": data.name,
		"author": data.author,
		"num": 1,
		"checkVal": false,
		"book_source_id": data.book_source_id,
		"desc": data.desc
	};
	var ifsuccess = false;
	let value = uni.getStorageSync('addshujiaData');
	console.log(value);
	if (value.length == 0) {
		value = [info];
		ifsuccess = true;
	} else {
		var ifSave = true;
		for (var i = 0; i < value.length; i++) {
			if (value[i].book_id == info.book_id) {
				ifSave = false;
			}
		}
		if (ifSave) {
			value.unshift(info);
			uni.showToast({
				title: '加入成功！'
			})
			ifsuccess = true;
		} else {
			uni.showToast({
				title: '本书已经在书架！',
				icon: 'none'
			})
		}
	}
	uni.removeStorageSync('addshujiaData')
	uni.setStorageSync('addshujiaData', value);
	return ifsuccess;
}
// 浏览记录
const looked = (obj) => {
	let values = uni.getStorageSync('lookeds')
	if (values == '') {
		values = []
	}
	let newArr = values.filter(item => item.book_id != obj.book_id)
	newArr.unshift(obj)
	uni.setStorageSync('lookeds',newArr)
	changeShujia(obj)
}
// 修改书架记录
const changeShujia = (obj) => {
	console.log(obj);
	let value = uni.getStorageSync('addshujiaData');
	if (value.length == 0) {
		return false
	} else {
		var ifchange = false;
		for (var i = 0; i < value.length; i++) {
			if (value[i].book_id == obj.book_id) {
				ifchange = true;
			}
		}
		if(ifchange){
			let newArr = value.filter(item => item.book_id != obj.book_id);
			newArr.unshift(obj);
			uni.removeStorageSync('addshujiaData')
			uni.setStorageSync('addshujiaData', newArr);
		}
	}
}
// 参数排序加密处理
const sortObj = (obj) => {
	let arr = [];
	for (let i in obj) {
		arr.push([i, obj[i]]);
	}
	arr.sort();
	let len = arr.length;
	let str = ''
	for (let i = 0; i < len; i++) {
		str += arr[i][0] + '=' + arr[i][1] + '&'
	}
	let md5Data = `${MD5(str.substring(0,str.length - 1))}&b4m5g09b2a734a4skc042b13897e4354`;
	let sign = MD5(md5Data)

	return sign;
}
// 图片加密处理
const imgMd5 = (str) => {
	const secret = 'xiakexing2017xhs';
	const time = (Date.parse(new Date()) + 60 * 60).toString();
	const reg = /^http.*?(.com|.net|.cn)\/(.*?)$/
	const upt = '/' + str.match(reg)[2]
	const uptMd5 = MD5(`${secret}&${time}&${upt}`)
	const upt1Md5 = uptMd5.substr(12, 8)
	const upt2Md5 = `${upt1Md5}${time}`
	str = `${str}?_upt=${upt2Md5}`
	return str;
}

// private property  
var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

// public method for encoding  
const encode = function(input) {
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;
	input = _utf8_encode(input);
	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
		output = output +
			_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
			_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
	}
	return output;
}

// public method for decoding  
const decode = function(input) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	while (i < input.length) {
		enc1 = _keyStr.indexOf(input.charAt(i++));
		enc2 = _keyStr.indexOf(input.charAt(i++));
		enc3 = _keyStr.indexOf(input.charAt(i++));
		enc4 = _keyStr.indexOf(input.charAt(i++));
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		output = output + String.fromCharCode(chr1);
		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
	}
	output = _utf8_decode(output);
	return output;
}
// private method for UTF-8 encoding  
const _utf8_encode = function(string) {
	string = string.replace(/\r\n/g, "\n");
	var utftext = "";
	for (var n = 0; n < string.length; n++) {
		var c = string.charCodeAt(n);
		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if ((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}
	return utftext;
}

// base64转化  
const _utf8_decode = function(utftext) {
	var string = "";
	var i = 0;
	var c = 0,
		c1 = 0,
		c2 = 0,
		c3;
	while (i < utftext.length) {
		c = utftext.charCodeAt(i);
		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		} else if ((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i + 1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = utftext.charCodeAt(i + 1);
			c3 = utftext.charCodeAt(i + 2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}
	return string;
}

export default {
	sortObj,
	imgMd5,
	encode,
	decode,
	addshujia,
	looked
}
